home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtool17.zip / STRINGS.H < prev    next >
C/C++ Source or Header  |  1993-09-20  |  19KB  |  731 lines

  1. /*
  2.  *  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)
  3.  *
  4.  * File         : strings.h
  5.  *
  6.  * Description  : strings management
  7.  *
  8.  */                       
  9.  
  10.  
  11. #ifndef __Strings_H
  12. #define __Strings_H
  13.  
  14.  
  15.                 /*  INCLUDE FILES  */
  16.  
  17. #include "c2cpp.h"
  18. #include <string.h>
  19. #include <stddef.h>
  20.  
  21.  
  22.  
  23. /***
  24.  *  Function    :   foreach
  25.  *
  26.  *  Description :   Loop on each token of a string
  27.  *
  28.  *  Parameters  :   out    char *word    pointer that will contain word (token)
  29.  *                  in/out char *string  string to tokenize
  30.  *
  31.  *  Side-effects:   Macro, so be careful.
  32.  *                  '\0' will be inserted into string.
  33.  *
  34.  *  Return code :   none
  35.  ***/
  36.  
  37. #define foreach( word , string ) \
  38.     for ( word = strtok(string," ") ; word ; word = strtok(NULL," ") )
  39.  
  40.  
  41.  
  42.  
  43. /***
  44.  *  Function    :   STRMIN/STRMAX
  45.  *
  46.  *  Description :   return min/max of two strings
  47.  *
  48.  *  Parameters  :   in   char *str1
  49.  *                  in   char *str2
  50.  *
  51.  *  Side-effects:   Macro, so be careful.
  52.  *
  53.  *  Return      :   pointer to min/max of the two strings
  54.  ***/
  55.  
  56. #define STRMIN( str1 , str2 )        ( (strcmp(str1 , str2) < 0) ? str1 : str2 )
  57. #define STRMAX( str1 , str2 )        ( (strcmp(str1 , str2) > 0) ? str1 : str2   )
  58.  
  59. /***
  60.  *  Function    :   strisequal
  61.  *
  62.  *  Description :   Test equality of two strings
  63.  *
  64.  *  Parameters  :   in   char *str1
  65.  *                  in   char *str2
  66.  *
  67.  *  Return      :   0 or 1
  68.  ***/
  69.  
  70. #define strisequal( string1 , string2 )    ( ! strcmp(string1 , string2) )
  71.  
  72.  
  73.     /***   For switch/case-like string manipulation
  74.  
  75.                usage:   STRWITCH( string )
  76.                                 {
  77.                                   STRCASE "string1": ... ;
  78.                                   STRCASE "string2": ... ; break ;
  79.                                   ...
  80.                                   STRDEFAULT    : ... ;
  81.                                 } STRENDSWITCH
  82.      ***/
  83.  
  84. #define STRSWITCH( _str )     { char *_tmp_s = _str ; \
  85.                                 int _tmp_flag = 0 ; \
  86.                                 do { if ( _tmp_flag )
  87.  
  88. #define STRCASE( _str )       ( _tmp_flag = 1 ) ; } \
  89.                               if ( _tmp_flag || ! strcmp(_tmp_s, _str) ) { 0 ? 0
  90.  
  91. #define STRDEFAULT            0 ; } { 0 ? 0
  92.  
  93. #define STRENDSWITCH          } while (0) ; }
  94.  
  95.  
  96.  
  97.  
  98.          /*  TYPES DEFINITIONS  */
  99.  
  100.  
  101. typedef enum { align_left , align_right , align_center } align_type ;
  102.  
  103. typedef enum { LOWER = 5 , UPPER } casetype ;
  104.  
  105.  
  106.  
  107.         /*  FUNCTIONS DEFINITIONS  */
  108.  
  109.  
  110. /***
  111.  *  Function    :  strleft
  112.  *
  113.  *  Description :  Copy the first ... characters of a string.
  114.  *           Like strncpy but add a '\0' at the end of the output string.
  115.  *
  116.  *  Decisions   :  If given length > string length : normal strcpy
  117.  *           If given length <= 0 returns an empty string.
  118.  *
  119.  *  Parameters  :  out  char  *out_str     result
  120.  *                 in   char  *in_str      in string
  121.  *                 in   int   length       length to be copied
  122.  *
  123.  *  Return code :   pointer to result.
  124.  *
  125.  *  OS/Compiler :   All
  126.  */
  127.  
  128. EXTERN char *strleft( char* , const char* , int ) ;
  129.  
  130.  
  131.  
  132.  
  133. /***
  134.  *  Function    :  strright
  135.  *
  136.  *  Description :  Copy the last ... characters of a string.
  137.  *
  138.  *  Decisions   :  If given length > string length : normal strcpy
  139.  *           If given length <= 0 returns an empty string.
  140.  *
  141.  *  Parameters  :  out  char  *out_str     result
  142.  *                 in   char  *in_str      in string
  143.  *                 in   int   length       length to be copied
  144.  *
  145.  *  Return code :   pointer to result.
  146.  *
  147.  *  OS/Compiler :   All
  148.  */
  149.  
  150. EXTERN char *strright( char* , const char* , int ) ;
  151.  
  152.  
  153.  
  154. /***
  155.  *  Function    :  strmid
  156.  *
  157.  *  Description :  Copy n characters of a string, begining at a given position
  158.  *           ( form 1 to ... )
  159.  *
  160.  *  Decisions   :  Stops at the end of input string if given length
  161.  *           is too big or length = 0.
  162.  *           If given length or position < 0 returns an empty string.
  163.  *
  164.  *  Parameters  :  out  char  *out_str     result
  165.  *                 in   char  *in_str      in string
  166.  *                 in   int   pos          position where begin to copy
  167.  *                 in   int   length       length to be copied
  168.  *
  169.  *  Return code :   pointer to result.
  170.  *
  171.  *  OS/Compiler :   All
  172.  */
  173.  
  174. EXTERN char *strmid( char* , const char* , int , int ) ;
  175.  
  176.  
  177.  
  178. /***
  179.  *  Function    :  stralign
  180.  *
  181.  *  Description :  Copy an input string in an output string
  182.  *           with specified alignement (blank padding).
  183.  *
  184.  *  Decisions   :  If given length < 0 returns an empty string.
  185.  *
  186.  *  Parameters  :  out  char        *out_str     result
  187.  *                 in   char        *in_str      in string
  188.  *                 in   align_type  casetype     type
  189.  *                 in   int         length       length to be copied
  190.  *
  191.  *  Value       :  type = { align_left, align_center, align_right }
  192.  *
  193.  *  Return code :   pointer to result.
  194.  *
  195.  *  OS/Compiler :   All
  196.  */
  197.  
  198. EXTERN char *stralign( char* , const char* , align_type , int ) ;
  199.  
  200.  
  201.  
  202. /***
  203.  *  Function    :  strmvstr
  204.  *
  205.  *  Description :  Copy an input string in an output string
  206.  *           with replacing all occurences of a target string.
  207.  *
  208.  *  Parameters  :  out      char  *out_str    result
  209.  *                 in       char  *in_str     in string
  210.  *                 in       char  *target     target string to replace
  211.  *                 in       char  *new_str    string to put in place of target
  212.  *
  213.  *  Return code :  pointer to result.
  214.  *
  215.  *  OS/Compiler :  All
  216.  ***/
  217.  
  218. EXTERN char *strmvstr( char* , const char* , const char* , const char* ) ;
  219.  
  220.  
  221.  
  222. /***
  223.  *  Function    :  strmvchr
  224.  *
  225.  *  Description :  Replace all occurences of a target character
  226.  *           by a new character.
  227.  *
  228.  *  Parameters  :  in/out   char  *string
  229.  *                 in       char  target     target char to replace
  230.  *                 in       char  new_char   char to put in place of target
  231.  *
  232.  *  Return code :  pointer to result.
  233.  *
  234.  *  OS/Compiler :  All
  235.  ***/
  236.  
  237. EXTERN char *strmvchr( char* , char , char ) ;
  238.                      
  239.  
  240.  
  241. /***
  242.  *  Function    :  strrmstr
  243.  *
  244.  *  Description :  Removing all occurences of a target string.
  245.  *
  246.  *  Parameters  :  in/out   char  *string
  247.  *                 in       char  *target     target string to remove
  248.  *
  249.  *  Decisions   :  Same implementation as strmvstr without copying
  250.  *           a replacement string.
  251.  *           Could also be implemented as
  252.  *                        strmvstr( ptr, ptr, target, "" )
  253.  *           but should be less efficient.
  254.  *
  255.  *  Return code :  pointer to result.
  256.  *
  257.  *  OS/Compiler :  All
  258.  ***/
  259.  
  260. EXTERN char *strrmstr( char* , const char* ) ;
  261.  
  262.  
  263.  
  264. /***
  265.  *  Function    :  strrmchr
  266.  *
  267.  *  Description :  Removing all occurences of a target character.
  268.  *
  269.  *  Parameters  :  in/out   char  *string
  270.  *                 in       char  target     target char to remove
  271.  *
  272.  *  Return code :  pointer to result.
  273.  *
  274.  *  OS/Compiler :  All
  275.  ***/
  276.  
  277. EXTERN char *strrmchr( char* , char ) ;
  278.  
  279.  
  280.  
  281. /***
  282.  *  Function    :  strinsert
  283.  *
  284.  *  Description :  Insert a string in another.
  285.  *
  286.  *  Parameters  :  out  char   *out_str    out string
  287.  *                 in   char   *in_str     in string
  288.  *                 in   char   *to_insert  in string to insert into the other
  289.  *                 in   int    place       place to insert string
  290.  *
  291.  *  Decisions   :  Does nothing if place specified out of range.
  292.  *
  293.  *  Return      :  pointer to result
  294.  *
  295.  *  OS/Compiler :  All
  296.  ***/
  297.  
  298. EXTERN char *strinsert( char* , const char* , const char* , int ) ;
  299.  
  300.  
  301.  
  302. /***
  303.  *  Function    :  strend
  304.  *
  305.  *  Description :  Returns a pointer to the '\0' ending a string
  306.  *
  307.  *  Parameters  :  in   char        *in_str      in string
  308.  *
  309.  *  Return code :   pointer to the '\0' ending the string..
  310.  *
  311.  *  OS/Compiler :   All
  312.  ***/
  313.     
  314. EXTERN char *strend( const char* ) ;
  315.  
  316.  
  317.  
  318. /***
  319.  *  Function    :   strskip
  320.  *
  321.  *  Description :   Skip 'n' words from a string.
  322.  *
  323.  *  Decisions   :
  324.  *
  325.  *  Parameters  :   in